home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / prolog1.arc / DEMO.DOC < prev    next >
Encoding:
Text File  |  1985-07-23  |  57.2 KB  |  2,179 lines

  1.  
  2.          
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.                      A.D.A PROLOG Documentation Version 1.5
  29.                   for the Educational and Public Domain Versions
  30.  
  31.                                   June 23, 1985
  32.  
  33.               Copyright Robert Morein and Automata Design Associates
  34.  
  35.                                  1570 Arran Way
  36.                                Dresher, Pa. 19025
  37.  
  38.                                  (215)-646-4894
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                                 Copyright Notice
  69.  
  70.  
  71.  
  72.              The  public domain PD PROLOG system has been contributed  to 
  73.         the  public domain for unrestricted use with one  exception:  the 
  74.         object  code  may not be  disassembled  or  modified.  Electronic 
  75.         bulletin  boards  and SIG groups are urged to aid in giving  this 
  76.         software the widest possible distribution.
  77.  
  78.              This documentation may be reproduced freely,  but it may not 
  79.         be  included in any other documentation without the permission of 
  80.         the author.
  81.  
  82.          
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                                   Introduction
  135.                 
  136.              We  are  pleased to present the second major version  of  PD 
  137.         PROLOG, version 1.5. The memory requirements are somewhat greater 
  138.         than the original,  since it is uses the large memory  model.  It 
  139.         compensates in thoroughness. The memory requirement is about 200K 
  140.         bytes  of  TPA,  and it will benefit from up to 240k  bytes.  The 
  141.         availalble workspace is 100K bytes.
  142.  
  143.  
  144.         We  hope  that you'll get some fun out of this  PROLOG.  It  will 
  145.         afford  you exposure to THE fifth generation language at the cost 
  146.         only  of  some  intellectual  effort.  The  motive  is  perfectly 
  147.         explicable:  We  want you to think of Automata Design  Associates 
  148.         for  fifth  generation  software.  It also gives us a  nice  warm 
  149.         feeling.
  150.              The  memory requirement is 200 k of transient program  area, 
  151.         plus  whatever  space is needed to execute programs  from  within 
  152.         PROLOG.  DOS  or  MSDOS 2.0 are required.  There are no  features 
  153.         requiring IBM PC architecture.
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.                      Products by Automata Design Associates
  201.  
  202.              Automata  Design  Associates  specializes  in  software  for 
  203.         artificial  intelligence  and  robotic  applications.   A  PROLOG 
  204.         language  system is available in various configurations.  A  LISP  
  205.         interpreter will be introduced in March of 1985.
  206.  
  207.  
  208.              There  are five versions of PROLOG available  from  Automata 
  209.         Design  Associates.  All  of  them run under the MSDOS  or  PCDOS 
  210.         operating systems. Other environments will be supported soon.
  211.  
  212.  
  213.              .Public Domain PROLOG
  214.  
  215.         This  serves to further the general awareness of the public about 
  216.         PROLOG.  It  also is an excellent adjunct to anyone learning  the 
  217.         language.  Most  of  the core PROLOG described  by  Clocksin  and 
  218.         Mellish  in the book Programming In PROLOG is implemented.  Trace 
  219.         predicates  and  I/O  redirection  are  not.   
  220.  
  221.  
  222.              .Educational PROLOG
  223.  
  224.         At  extremely modest cost this affords an educational institution 
  225.         or  individual  a  PROLOG  system  which  provides  the   maximum 
  226.         available  programming  area  available  within  the  8086  small 
  227.         programming model.  Tracing,  a debugging aid,  allows monitoring 
  228.         a program as it runs.  User settable spy points selectively allow 
  229.         this.  Exhaustive  tracing  is also  available.  I/O  redirection 
  230.         gives some file ability.
  231.  
  232.              An  "exec"  function allows the execution of  a  program  or 
  233.         editor  from  within  PROLOG,  thus  encouraging  an  interactive 
  234.         environment.
  235.  
  236.              Definite clause grammar support is now included.
  237.              
  238.              The cost of Educational PROLOG is $29.95.
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.              .FSM PROLOG
  269.  
  270.              A  small  increment  in price adds full random  access  file 
  271.         capability. Character and structure I/O are allowed.
  272.              The  "asserta and "assertz" predicates are expanded and work 
  273.         with a clause indexing ability.  One can assert clauses  anywhere 
  274.         in the database under precise pattern matching control. 
  275.  
  276.              A tree structured lexical scoping system and floating  point 
  277.         arithmetic are other enhancements.
  278.  
  279.              The cost of FSM PROLOG is $49.95
  280.  
  281.  
  282.  
  283.              .VMI PROLOG -- Virtual Memory (Replaces type VMS)
  284.  
  285.  
  286.              At reasonable cost the addition of virtual memory gives  an 
  287.         expansion of capabilities of an order of magnitude. 
  288.  
  289.              The  database on disk is treated transparently.  No  special 
  290.         provisions  need  be  made  by the  user.  Virtual  and  resident 
  291.         databases  may be mixed.  A unique updating algorithim  preserves 
  292.         the format of the database as typed by the user while making only 
  293.         those changes necessary to make it equivalent to the database  in 
  294.         central memory.
  295.  
  296.              The cost of VMI PROLOG is $99.95
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.              .VML PROLOG Large model Virtual Memory System
  333.  
  334.              A.D.A.  PROLOG is a remarkable fifth generation developement 
  335.         tool  for  the  implementation  of  intelligent  strategies   and 
  336.         optimized  control.  It  is both the kernel for  applications  of 
  337.         virtually  unlimited scope and a sophisticated developement  tool 
  338.         that multiplies the productivity of the programmer many times. 
  339.  
  340.              With  a  cost/performance ratio exceeding that of any  other 
  341.         product  and  a  compatibility  insured  by  compliance  to   the 
  342.         Edinburgh syntax, performance is enhanced by numerous extensions, 
  343.         many of them invisible to the user.
  344.  
  345.              A quick overview of some of the features discloses:
  346.  
  347.              1) Invisible compilation to a semantic network preserves the 
  348.              flexibility  of  the  interpreted mode and the  speed  of  a 
  349.              compiler.
  350.                   The programmer can compile and recompile any portion of 
  351.              a  module at any time.  The edit/compile/test cycle is short 
  352.              and free of strain. An interface is provided to an editor of 
  353.              choice.
  354.  
  355.              2) Floating point arithmetic with a full complement of input 
  356.              and output methods, transcendental and conversion functions.
  357.  
  358.              3)  Virtual memory.  Module size and number is unrestricted. 
  359.              Resident and virtual modules may be coresident.  Compilation 
  360.              is incremental. The cache algorithim is sophisticated.
  361.                   Changes  made in the database can be updated to disk by 
  362.              a single command.
  363.  
  364.              4) A powerful exec function,  and acceptance of stream input 
  365.              make integration into applications practical.
  366.  
  367.              5) Many   additional  built-in  predicates   enhance   the 
  368.              efficiency of the system.
  369.  
  370.              6) Debugging facilities let you see your program run without 
  371.              any additional generation steps.
  372.  
  373.              7)  Totally  invisible and incremental  garbage  collection. 
  374.              There is NEVER any wait for this function.
  375.  
  376.         The  cost  of this system is $200 for the MSDOS version.
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.                                  Upgrade Policy
  399.  
  400.              Half  the  cost  of any A.D.A.  PROLOG  interpreter  may  be 
  401.         credited to the purchase of a higher level version.
  402.              The  full cost of VMS prolog may be applied to the  purchase 
  403.         of VMI or VML PROLOG.
  404.              Updates  to  a particular level product vary from $15.00  to 
  405.         $35.00.
  406.  
  407.  
  408.                                 Run-time Packages
  409.  
  410.              Software  developers wishing to integrate an A.D.A.  product 
  411.         into  their  system  should inquire  about  specialized  run-time 
  412.         packages available at reasonable cost.
  413.  
  414.  
  415.                               To Place Your Order:
  416.  
  417.              The phone for orders is (215)-355-5400.  Visa, Mastercharge, 
  418.         and American Express are accepted.
  419.  
  420.  
  421.                               Technical Information
  422.  
  423.              Technical information may be obtained at (215) - 646- 4894
  424.  
  425.              Perhaps we can answer the following questions in advance:
  426.  
  427.              There is no support for:  APPLE II, Atari, Commodore, or  
  428.         CPM 80 .  Other machines from these manufactures may be supported 
  429.         in the future.
  430.  
  431.              The   public   domain   version  is   available   from   the 
  432.         organizations  who distribute such software.  It is not available 
  433.         from us.
  434.              The MSDOS products are available on 5" and 8" diskettes.
  435.              
  436.  
  437.                                      Returns
  438.  
  439.              The  software may be returned within 30 days of purchase for 
  440.         a full refund.  This applies whether or not the software has been 
  441.         used.  We do ask that manuals, disks and packaging be returned in 
  442.         excellent condition.
  443.  
  444.  
  445.                               To Place Your Order:
  446.  
  447.              You may place your order at either of the following numbers:
  448.  
  449.              (215)-646-4894   - day and night.
  450.  
  451.              (215)-355-5400   - 9 A.M. to 5 P.M., Eastern Standard Time
  452.  
  453.              Resellers should call (215)-646-4894 for information.
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.                       How to run the Demonstration Programs 
  465.                         without Knowing What You're Doing
  466.  
  467.              We strongly advise that you purchase the book Programming in 
  468.         PROLOG by Clocksin and Mellish,  publisher Springer Verlag, 1981. 
  469.         For  the  impatient we give some advice.  Type the  demonstration 
  470.         program you wish to run.  There must be at least one entry  point 
  471.         within  the  program.  
  472.  
  473.         Note:  Please  understand that these are demonstrations programs. 
  474.         Regarding  user  interface,  they are poorly  written.  You  will 
  475.         probably have to read Clocksin and Mellish to appreciate that the 
  476.         following examples of input are not equivalent: "yes." , "yes" .
  477.  
  478.  
  479.         The animals program - "animal"
  480.          
  481.              Most  of the examples require C & M for  comprehension.  The 
  482.         program "animals", however, can be appreciated by anyone. It is a 
  483.         traditional   example of an expert system.
  484.              
  485.              We had hoped to include the animals program on disk,  but we 
  486.         have  found  to  our  dismay that the version which  we  used  is 
  487.         allegedly copyrighted by the implementors of PROLOG 86.  Don't be 
  488.         surprised - even "happy birthday" is copyrighted.  We will simply 
  489.         point out that the November '84 issue of Robotics Age included  a 
  490.         version  of  the  animals game,  which you can,  at the  risk  of 
  491.         copyright infringement,  type in.  There is only one change  that 
  492.         need  be  made.  The "concat" function used in that  program  has 
  493.         arguments of the form:
  494.  
  495.                       concat( [atom1, atom2,...], result ).
  496.  
  497.         In order to make the concat definition more closely resemble that 
  498.         of  "name",  which  is  described by Clocksin  and  Mellish,  the 
  499.         argments have been reversed:
  500.  
  501.                       concat( result, [atom1, atom2,...] )
  502.  
  503.         Assuming  that you have typed in the program and made the  change 
  504.         just noted, the following steps are required to run it:
  505.              
  506.              Run the prolog.exe file.  The prompt "?-" will appear.  Type 
  507.         "consult( 'animals' ).<CR>".  Here <CR> indicates you are to type 
  508.         a  carriage  return.  The PROLOG system will load  "animals"  and 
  509.         compile  it into an internal form.  When the "?-" prompt  appears 
  510.         PROLOG is ready to run the "animals" guessing game. The object of 
  511.         the program is to deduce the animal you are thinking of. To start 
  512.         it  off  type  "help.<CR>".  PROLOG  will  respond  by  asking  a 
  513.         question. 
  514.              Because of the way the animals program is written,  you must 
  515.         respond in a rigid format.  You may type "yes<CR>",  "no<CR>", or 
  516.         "why<CR>". 
  517.              Eventually the program will terminate with either a guess as 
  518.         to what animal you are thinking of,  or a remark that the  animal 
  519.         is  not within its domain of knowledge.  The program has learned, 
  520.         however.  You  may  run  the  program again to  see  what  effect 
  521.         additional knowledge has on the program's behavior. 
  522.  
  523.              The program fragment "console" shows how you may improve the 
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.         console input routines of any of these programs.
  531.              
  532.  
  533.         The Hematology Diagnosis Program - "hemat"
  534.  
  535.              Although  the  logical structure is not as sophisticated  as 
  536.         that of "animals", it is interesting for several reasons:
  537.  
  538.              1)  The  program  evaluates numerical data to  arrive  at  a 
  539.         diagnosis.
  540.  
  541.              2) Although inaccurate, it demonstrates that useful question 
  542.         answering systems are not difficult to write in PROLOG.
  543.  
  544.              3)  There  are  some mistakes in  the  program,  which  only 
  545.         slightly impede its usefulness. 
  546.  
  547.              This  program  uses  structure  input.  Terminate  all  your 
  548.         answers with a period, as in "y.<CR>", or "no.<CR>".
  549.  
  550.              The starting point is "signs.<CR>".  PROLOG will prompt  you 
  551.         for  signs  of  anemia.  The  program attempts  to  diagnose  two 
  552.         varieties of a hemolytic anemia.
  553.              The program could use a good working over by a  hematologist 
  554.         and we would be delighted to collaborate.
  555.  
  556.  
  557.         Prime Number Generator - "sieve"
  558.  
  559.              This  program demonstrates that anything can be programed in 
  560.         PROLOG if one tries hard enough.  Asking the  question   
  561.           "primes( 50, L ).<CR>" causes a list of prime numbers less than 
  562.         50  to be printed out.  "Sieve" is heavily recursive and  quickly 
  563.         exhausts the stack space of the small model interpreters.
  564.  
  565.  
  566.  
  567.  
  568.  
  569.  
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.                                   Special Offer
  597.  
  598.              For  some  inexplicable reason,  demonstration programs  are 
  599.         hard to come by. We are too busy writing PROLOG fill this gap. We 
  600.         will  reward the contribution of "cute" sample programs with  the 
  601.         following:
  602.  
  603.              1) A free  copy of type VMS virtual memory PROLOG
  604.  
  605.              2) The sample program will be published as an intact file 
  606.              together with whatever comments or advertisments the author
  607.              may see fit to include, on our distribution disks. 
  608.  
  609.              3) Exceptional contributions may merit a copy of type VML 
  610.              large  model virtual memory PROLOG which now incorporates  a 
  611.              UNIX1 style tree structured domain system.
  612.  
  613.  
  614.  
  615.         ______
  616.              1. Trademark of AT & T.
  617.  
  618.  
  619.  
  620.  
  621.  
  622.  
  623.  
  624.  
  625.  
  626.  
  627.  
  628.  
  629.  
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636.  
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.                          Writing Programs For ED PROLOG
  663.  
  664.              You do not type in programs at the "?-" prompt.  There is no 
  665.         built-in  editor.  The command "consult( user )" is accepted  but 
  666.         does not cause PROLOG to enter an editing mode.  We feel that the 
  667.         most universally acceptable editing method is for the user to use 
  668.         a text editor of choice,  which can be invoked from within PROLOG 
  669.         by the "exec" predicate.
  670.  
  671.              Use  Wordstar  or your customary editor to write a  program. 
  672.         Then  run  PD  PROLOG and use the consult function  to  load  the 
  673.         program.
  674.  
  675.              In  all  cases except PD PROLOG,  you can  run  your  editor 
  676.         without leaving PROLOG by use of the "exec" predicate.
  677.  
  678.  
  679.  
  680.                              Running the Interpreter
  681.  
  682.         COMMANDS: Give commands in lower case.
  683.  
  684.         TO RUN:
  685.              Invoke PROLOG.EXE. After the "?-" prompt appears,
  686.                type "consult( <filename><CR> )", where <filename> is the 
  687.                desired database.
  688.              To exit, type "exitsys.<CR>"
  689.  
  690.         TO ASK A QUESTION:
  691.              At the prompt, type "<expression>.<CR>", where 
  692.              <expression>  is  a  question as described by  Clocksin  and 
  693.              Mellish. Be sure to terminate the question with a period.
  694.              The question may be up to 500 characters long.
  695.  
  696.         TO INPUT A STRUCTURE AT THE KEYBOARD:
  697.              The structure may be up to 500 characters in length. Be sure 
  698.              to terminate with a period.
  699.  
  700.         TO ASK FOR ANOTHER SOLUTION:
  701.              If a solution has been provided, the PROLOG interpreter will 
  702.              ask  "More?  (Y/N):".  Only  if  a "y"  is  typed  will  the 
  703.              interpreter perform a search.
  704.  
  705.         TO ABORT A SEARCH:
  706.              Simply   type   the  escape  key.   The   interpreter   will      
  707.              respond  with  "Interrrupted.",  and return to  the  command      
  708.              prompt.
  709.  
  710.         TO LOAD ANOTHER DATABASE:
  711.              Type  "consult(<filename>).<CR>" The file name must have the 
  712.              extension  ".PRO".  It  is  not  necessary  to  include  the 
  713.              extension in the argument of consult.
  714.  
  715.         TO REMOVE A DATABASE:
  716.              Type "forget(<filename>).<CR>"
  717.  
  718.         TO EXIT TO THE OPERATING SYSTEM:
  719.              Type "exitsys.<CR>"
  720.  
  721.              The system is totally interactive; any commands the operator 
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728.         gives  are and must be valid program statements.  Statements must 
  729.         terminate with a period.      All commands which take a file name 
  730.         also  accept a path name.  Any name which is not a  valid  PROLOG 
  731.         atom (refer to C & M) must be enclosed in single quotes. Thus one 
  732.         could say
  733.  
  734.              consult( expert )
  735.  
  736.         but one would need single quotes with
  737.  
  738.              consult( 'b:\samples\subtype\expert' ).
  739.  
  740.  
  741.         To exit the system, type "exitsys.<CR>"
  742.  
  743.         Atoms  may contain MSDOS pathnames if they are enclosed by single 
  744.         quotes, ie.,  '\b:\samples\animal' .
  745.  
  746.         You may consult more than one file at a time.  However, all names 
  747.         are public and name conflicts must be avoided. The order in which 
  748.         modules are loaded may,  in cases of poor program design,  affect 
  749.         the behavior.
  750.  
  751.  
  752.  
  753.  
  754.                              Command Line Arguments
  755.  
  756.              ED  PROLOG accepts one command line argument,  which is  the 
  757.         name  of  a "stream" which replaces the console  for  input.  The 
  758.         "stream"  in  MSDOS is a pipe or file which supplies input  until 
  759.         end-of-file is reached. Control then reverts back to the console. 
  760.         To avoid noisy parser error messages when end-of-file is reached, 
  761.         the last command in the file should be "see( user )."
  762.  
  763.  
  764.  
  765.                                A Reference of Note
  766.  
  767.  
  768.              With  minor  exceptions,  the syntax is a superset  of  that 
  769.         described  by  Clocksin and Mellish in the  book  Programming  in 
  770.         Prolog by W.F.  Clocksin and C.S.  Mellish, published by Springer 
  771.         Verlag in Berlin, Heidelberg, New York, and Tokyo. We shall refer 
  772.         to this book as "C & M".
  773.  
  774.  
  775.              There   are  very  few   syntactical   differences,   mostly 
  776.         unrecognized and/or minor.
  777.              When  an operator is declared using the "op" statement,  the 
  778.         operator must be enclosed in single quotes in the "op"  statement 
  779.         itself,  if  it would not otherwise be a legal Edinburgh functor. 
  780.         Subsequently,  however,  the parser will recognize it for what it 
  781.         is,  except  in  the "unop" statement,  where it  must  again  be 
  782.         enclosed in single quotes.
  783.  
  784.              Variable numbers of functor paramaters are supported.
  785.  
  786.              A  goal  may be represented by a  variable,  which  is  less 
  787.         restrictive  than  the  C  &  M requirement  that  all  goals  be 
  788.  
  789.  
  790.  
  791.  
  792.  
  793.  
  794.         functors.  The  variable must be instantiated to a  functor  when 
  795.         that goal is pursued.
  796.  
  797.              Rules which appear inside other expressions must be enclosed 
  798.         in  parenthesis  if  the "," operator is to be  recognized  as  a 
  799.         logical connective.
  800.  
  801.              All  infix  operators described by C & M,  and user  defined 
  802.         infix,  prefix, and postfix operators with variable associativity 
  803.         and precedence are supported exactly as in C & M.
  804.  
  805.  
  806.  
  807.  
  808.  
  809.  
  810.  
  811.  
  812.  
  813.  
  814.  
  815.  
  816.  
  817.  
  818.  
  819.  
  820.  
  821.  
  822.  
  823.  
  824.  
  825.  
  826.  
  827.  
  828.  
  829.  
  830.  
  831.  
  832.  
  833.  
  834.  
  835.  
  836.  
  837.  
  838.  
  839.  
  840.  
  841.  
  842.  
  843.  
  844.  
  845.  
  846.  
  847.  
  848.  
  849.  
  850.  
  851.  
  852.  
  853.  
  854.  
  855.  
  856.  
  857.  
  858.  
  859.  
  860.                          The Built In Predicate Library
  861.  
  862.  
  863.              The  following built in predicates described by Clocksin and 
  864.         Mellish   are  supported.   Those  marked  with  asterisks   have 
  865.         additional features:
  866.              
  867.              arg
  868.              arithmetic operators:  "+", "-", "*", "/", <, <=, >, >=, mod
  869.             *asserta
  870.             *assertz
  871.              atom
  872.              atomic
  873.              call
  874.             *clause
  875.              clearops
  876.              consult
  877.              !
  878.             *debugging
  879.             *display
  880.              fail
  881.              functor
  882.             *get0
  883.              integer
  884.              is
  885.              list extraction operator: "|"
  886.              listing
  887.              logical connectives:  ",", ";",
  888.              mod
  889.              name
  890.              nl
  891.              nodebug
  892.              nonvar
  893.              not
  894.              notrace
  895.              op
  896.             *put
  897.             *read
  898.             *reconsult
  899.              repeat
  900.              retract
  901.              see
  902.              seeing
  903.              seen
  904.              spy
  905.              tab
  906.              tell
  907.              telling
  908.              told
  909.              trace
  910.              true
  911.              var
  912.  
  913.  
  914.  
  915.  
  916.  
  917.  
  918.  
  919.  
  920.  
  921.  
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.                         Description of the Modifications.
  929.              
  930.  
  931.         call( <goal> )
  932.  
  933.              The  predicate as defined in C & M is obsolete.  The purpose 
  934.         was  to permit a goal search where the goal name was  a  variable 
  935.         instantiated  to  some functor name.  A.D.A.  permits writing  of 
  936.         goals with such names,  so the mediation of the "call" clause  is 
  937.         no longer necessary.
  938.  
  939.              The  "call"  predicate  may  be  trivially  implemented  for 
  940.         compatibility with the PROLOG definition
  941.  
  942.                                  call( X ) :- X.
  943.  
  944.  
  945.  
  946.         clause
  947.  
  948.              The  function  clause(  X,  Y ) has the  new  optional  form 
  949.         clause(  X,  Y,  I  ).  If the third variable is written,  it  is 
  950.         instantiated  to the current address of a clause in  memory.  The 
  951.         only  use of the result is with succeeding assertfa and  assertfz 
  952.         statements.
  953.  
  954.  
  955.  
  956.         cut
  957.              If  the cut is the last clause of a search the user will not 
  958.         be queried "More? Y/N" for additional solutions.
  959.  
  960.  
  961.  
  962.         debugging
  963.  
  964.              "Debugging"  prints a list of the current  spypoints.  After 
  965.         each name a sequence of numbers may appear, indicating the number 
  966.         of  arguments that is a condition of the trace.  The  word  "all" 
  967.         appears  if  the  number of arguments is not a condition  of  the 
  968.         trace.
  969.  
  970.  
  971.  
  972.  
  973.  
  974.  
  975.  
  976.  
  977.  
  978.  
  979.  
  980.  
  981.  
  982.  
  983.  
  984.  
  985.  
  986.  
  987.  
  988.  
  989.  
  990.  
  991.  
  992.         op( <prec>, <assoc>, <functor> )
  993.  
  994.              Defines  the  user  definable  grammar  of  a  functor.  The 
  995.         definition conforms to that in C & M. We mention here a minor but 
  996.         important point.  If <functor> is not a valid PROLOG atom it must 
  997.         be   enclosed  in  single  quotes  when  declared  in  the   "op" 
  998.         declaration and when undeclared in the "unop" declaration.  It is 
  999.         not  necessary  or legal to do this when the functor is  actually 
  1000.         being used as an operator.
  1001.  
  1002.              Declared  operators  are annotated in the directory  display 
  1003.         with their precedence and associativity.
  1004.  
  1005.  
  1006.         Output predicates
  1007.  
  1008.            display
  1009.            write
  1010.            print
  1011.            put
  1012.  
  1013.              These  functions  have  been  modified  to  accept  multiple 
  1014.         arguments in the form:
  1015.  
  1016.                        print( <arg1>, <arg2>, <arg3>,... )
  1017.  
  1018.         Thus, "put( a, b, c )" would result in the display of "abc".
  1019.              The  names  of  some  PROLOG atoms that may  occur  are  not 
  1020.         accepted  by  the  PROLOG scanner  unless  surrounded  by  single 
  1021.         quotes.  This only applies when such an atom is read in, not when 
  1022.         it is internally generated. Nevertheless, this presents us with a 
  1023.         problem:  We  would  like to be capable of writing  valid  PROLOG 
  1024.         terms to a file. In some cases, it is necessary to add the single 
  1025.         quotes.  In other cases,  such as human oriented output, they are 
  1026.         an irritant. The modified definitions of the following predicates 
  1027.         are an attempt at a solution:
  1028.  
  1029.              display
  1030.              Operator  precedence  is ignored,  all functors are  printed 
  1031.              prefix and single quotes are printed if needed or they  were 
  1032.              supplied if and when the atom was originally input.
  1033.  
  1034.              write
  1035.              Operator  precedence is taken into account and operators are 
  1036.              printed according to precedence.  Single quotes are  printed 
  1037.              under the same conditions as for "display."
  1038.  
  1039.              print
  1040.              Operator  precedence is taken into account and operators are 
  1041.              printed  according to precedence.  Single quotes  are  never 
  1042.              displayed.  This  is  a  human oriented form of  output  and 
  1043.              should  never  be used for writing of terms for  the  PROLOG 
  1044.              scanner.
  1045.  
  1046.              
  1047.  
  1048.  
  1049.  
  1050.  
  1051.  
  1052.  
  1053.  
  1054.  
  1055.  
  1056.  
  1057.  
  1058.         get0
  1059.         read
  1060.  
  1061.              The  functions  "get0"  and "read"  have  been  extended  to 
  1062.         support input from a stream other than the one currently selected 
  1063.         by "see". To direct output to a file or other stream, an optional 
  1064.         argument  is used.  For example,  "get0( char,  <file name> )" or 
  1065.         "get0( char,  user )" would cause input to come from <file  name> 
  1066.         or the console.  If the file has not already been opened,  "get0" 
  1067.         will fail.
  1068.  
  1069.  
  1070.  
  1071.              Atoms enclosed by single quotest, eg. '\nthis is a new line' 
  1072.         can contain the escape sequences
  1073.  
  1074.              '\n', '\r', '\t' and '\''.
  1075.  
  1076.              If these atoms are printed by "display" or "write" they  are 
  1077.         printed  just  as they are.  If they are printed by  the  "print" 
  1078.         clause they are translated as follows:
  1079.  
  1080.         '\n'  results  in  the printing of a carriage return and  a  line 
  1081.         feed.
  1082.         '\r' results in the printing of a carriage return only.
  1083.         '\t' results in the printing of a tab character.
  1084.         '\'' allows the printing of a single quote within a quoted atom.
  1085.  
  1086.              The "portray" feature is not presently implemented.
  1087.          
  1088.                         Description of the New Predicates
  1089.  
  1090.  
  1091.         clearops-
  1092.              Nullify  the  operator  status  of  every  operator  in  the 
  1093.              database.
  1094.  
  1095.         concat( (<variable> | <functor>), <List> )
  1096.              A  list  of functors or operators is concatenated  into  one 
  1097.         string,  which becomes the name of a new atom to which <variable> 
  1098.         or <functor> must match or be instantiated.
  1099.  
  1100.  
  1101.         dir( option )
  1102.              Provide  an  alphabetized listing to the console  of  atoms, 
  1103.         constants,   or   open  files.   Without  options,   simply  type 
  1104.         "dir.<CR>". Options are:
  1105.  
  1106.              dir( pred ) - list clause names only.
  1107.              dir( files ) - list open files only.
  1108.  
  1109.  
  1110.         exitsys
  1111.              Exit to the operating system.
  1112.  
  1113.         forget( <file name> )
  1114.              Make  a database unavailable for use and reclaim the storage 
  1115.         it occupied.
  1116.  
  1117.  
  1118.  
  1119.  
  1120.  
  1121.  
  1122.  
  1123.  
  1124.         ratom( <arg>, <stream> )-
  1125.              Read an atom from the input stream,  to which <arg>  matches 
  1126.         or  is  instantiated.  <stream> is optional.  If <stream> is  not 
  1127.         given, the input stream defaults to the standar input.
  1128.              Input is terminated by a CR or LF, which are not included in 
  1129.         the stream.
  1130.  
  1131.  
  1132.  
  1133.  
  1134.  
  1135.  
  1136.  
  1137.  
  1138.  
  1139.  
  1140.  
  1141.  
  1142.  
  1143.  
  1144.  
  1145.  
  1146.  
  1147.  
  1148.  
  1149.  
  1150.  
  1151.  
  1152.  
  1153.  
  1154.  
  1155.  
  1156.  
  1157.  
  1158.  
  1159.  
  1160.  
  1161.  
  1162.  
  1163.  
  1164.  
  1165.  
  1166.  
  1167.  
  1168.  
  1169.  
  1170.  
  1171.  
  1172.  
  1173.  
  1174.  
  1175.  
  1176.  
  1177.  
  1178.  
  1179.  
  1180.  
  1181.  
  1182.  
  1183.  
  1184.  
  1185.  
  1186.  
  1187.  
  1188.  
  1189.  
  1190.                              Arithmetic Capabilities
  1191.  
  1192.              Integer  arithmetic is supported.  Numbers are 32 bit signed 
  1193.         quantities. The following arithmetic operators are supported:
  1194.  
  1195.                       "+", "-", "*", "/", <, <=, >, >=, mod.
  1196.  
  1197.         Arithmetic operators must never be used as goals,  although  they 
  1198.         may be part of structures. It is legal to write:
  1199.  
  1200.                                     X = a + b
  1201.              
  1202.         which  results in the instantiation of X to the struture (a + b). 
  1203.         But the following is not legal:
  1204.  
  1205.                        alpha( X, Y ) :- X + Y, beta( Y ).
  1206.  
  1207.  
  1208.         Evaluation  of  an arithemtic expression is mediated by the  "is" 
  1209.         and inequality predicates.  For instance,  the following would be 
  1210.         correct:
  1211.  
  1212.                          alpha( X, Y, Z ) :- Z is X + Y.
  1213.  
  1214.                          beta( X, Y ) :- X + 2 < Y + 3.
  1215.  
  1216.  
  1217.  
  1218.  
  1219.  
  1220.  
  1221.  
  1222.  
  1223.  
  1224.  
  1225.  
  1226.  
  1227.  
  1228.  
  1229.  
  1230.  
  1231.  
  1232.  
  1233.  
  1234.  
  1235.  
  1236.  
  1237.  
  1238.  
  1239.  
  1240.  
  1241.  
  1242.  
  1243.  
  1244.  
  1245.  
  1246.  
  1247.  
  1248.  
  1249.  
  1250.  
  1251.  
  1252.  
  1253.  
  1254.  
  1255.  
  1256.                                  Prolog Tutorial          
  1257.  
  1258.  
  1259.                                   Introduction
  1260.  
  1261.  
  1262.  
  1263.  
  1264.              Probably  you  have heard of the language PROLOG within  the 
  1265.         last year or so. You probably wondered the following things:
  1266.  
  1267.         1) What does the name stand for? Names of computer languages are 
  1268.         almost always acronyms.
  1269.  
  1270.         2) What is it good for?
  1271.  
  1272.         3) Why now?
  1273.  
  1274.         4) Can I get a copy to play with?
  1275.  
  1276.              Congratulations! You obviously know the answer to the fourth 
  1277.         question. We now respond to the other three.
  1278.              
  1279.         1)  The  name  stands for "programming in logic." This  we  shall 
  1280.         elaborate on in depth later on.
  1281.  
  1282.         2) PROLOG is good for writing question answering systems.  It  is 
  1283.         also   good   for  writing  programs  that  perform   complicated 
  1284.         strategies  that  compute the best or worst way to  accomplish  a 
  1285.         task, or avoid an undesirable result.
  1286.  
  1287.         3) PROLOG was virtually unknown in this country until researchers 
  1288.         in  Japan announced that it was to be the core language  of  that 
  1289.         country's fifth generation computer project.  This is the project 
  1290.         with  which  Japan hopes to achieve a domainant position  in  the 
  1291.         world information industry of the 1990's. 
  1292.  
  1293.              PROLOG  is  one of the most unusual computer languages  ever 
  1294.         invented.  It  cannot be compared to  FORTRAN,  PASCAL,  "C",  or 
  1295.         BASIC.  The facilities complement,  rather than replace those  of 
  1296.         conventional  languages.  Although  it  has great  potential  for 
  1297.         database  work,  it  has  nothing in  common  with  the  database 
  1298.         languages used on microcomputers.
  1299.  
  1300.              Perhaps  the  best point to make is that while  conventional 
  1301.         languages are prescriptive, PROLOG is descriptive. A statement in 
  1302.         a conventional language might read:
  1303.  
  1304.              if( car_wheels = TRUE ) then
  1305.                begin
  1306.                  (some sort of procedure)
  1307.                  X = X + 1;
  1308.                end 
  1309.  
  1310.  
  1311.  
  1312.  
  1313.  
  1314.  
  1315.  
  1316.  
  1317.  
  1318.  
  1319.  
  1320.  
  1321.  
  1322.         A statment in PROLOG could just be a statment of fact about  cars 
  1323.         and wheels. There are many relationships that hold. For instance,
  1324.  
  1325.              has( car, wheels ).
  1326.  
  1327.              has( car, quant(wheels, four) ).
  1328.  
  1329.              round( wheels ).
  1330.  
  1331.         Each  of  these statments is an independent fact  relating  cars, 
  1332.         wheels,  and  the  characteristics of wheels.  Because  they  are 
  1333.         independent, they can be put into a PROLOG program by programmers 
  1334.         working separately. The man who is a specialist on car bodies can 
  1335.         say  his thing,  the wheel specialist can have his say,  and  the 
  1336.         participants can work with relative independence. And this brings 
  1337.         to light a major advantage of PROLOG:
  1338.  
  1339.  
  1340.                              PARALLEL PROGRAMMING!!!
  1341.                             
  1342.  
  1343.         With  conventional  programming languages projects can  still  be 
  1344.         "chunked",  or  divided between programmers.  But efficiency on a 
  1345.         team  project  drops  drastically below that  of  the  individual 
  1346.         programmer  wrapped  up  in  his own trance.  As  the  number  of 
  1347.         participants    grows   the   need   for   communication    grows 
  1348.         geometrically. The time spent communicating can exceed that spent 
  1349.         programming! 
  1350.              Although   PROLOG   does   not  eliminate   the   need   for 
  1351.         task  coordination,  the problem is considerably  simplified.  It 
  1352.         also provides the ability to answer questions in a "ready to  eat 
  1353.         form."  Consider your favorite BASIC interpreter.  Based upon the 
  1354.         statements about cars and wheels previously given,  could you ask 
  1355.         it the following question?   
  1356.  
  1357.                        
  1358.               has( car, X ), round( X ).
  1359.  
  1360.              Does  a  car  have anything which  is  round?  The  question 
  1361.         instructs the PROLOG interpreter to consider all the objects that 
  1362.         it  knows are possessed by a car and find those which are  round. 
  1363.         Perhaps  you are beginning to guess that PROLOG has the abilities 
  1364.         of a smart database searcher.  It can not only find the facts but 
  1365.         selectively find them and interpret them.
  1366.  
  1367.  
  1368.  
  1369.  
  1370.  
  1371.  
  1372.  
  1373.  
  1374.  
  1375.  
  1376.  
  1377.  
  1378.  
  1379.  
  1380.  
  1381.  
  1382.  
  1383.  
  1384.  
  1385.  
  1386.  
  1387.  
  1388.              Consider the problem of a fault tree, as exemplified by this 
  1389.         abbreviated one:
  1390.  
  1391.  
  1392.  
  1393.         {Car won't start}
  1394.              | 
  1395.              | 
  1396.         [Engine  turns  over](No) --> [Battery voltage](no)-\
  1397.                 (Yes)                                       v
  1398.                  |                                  {Check battery}
  1399.                  |
  1400.         [Smell gasoline](yes) --> {Try full throttle cranking}
  1401.                  |                       (failure)
  1402.         /--------/                           |
  1403.  
  1404.                             (details omitted)
  1405.  
  1406.  
  1407.  
  1408.              The fault tree is easily programmed in BASIC. Later we shall 
  1409.         show  that  PROLOG supplies a superior replacement for the  fault 
  1410.         tree.  Though the tree is capable of diagnosing only the  problem 
  1411.         for  which  it was designed,  PROLOG dynamically  constructs  the 
  1412.         appropriate  tree from facts and rules you have provided.  PROLOG 
  1413.         is not limited to answering one specific question.  Given  enough 
  1414.         information,  it  will attempt to find all deductive solutions to 
  1415.         any problem.
  1416.  
  1417.  
  1418.  
  1419.  
  1420.  
  1421.  
  1422.  
  1423.  
  1424.  
  1425.  
  1426.  
  1427.  
  1428.  
  1429.  
  1430.  
  1431.  
  1432.  
  1433.  
  1434.  
  1435.  
  1436.  
  1437.  
  1438.  
  1439.  
  1440.  
  1441.  
  1442.  
  1443.  
  1444.  
  1445.  
  1446.  
  1447.  
  1448.  
  1449.  
  1450.  
  1451.  
  1452.  
  1453.  
  1454.  
  1455.                                   PROLOG PRIMER
  1456.  
  1457.         I.                       Rules and Facts     
  1458.  
  1459.  
  1460.  
  1461.              This  is  where you should start if you know  nothing  about 
  1462.         PROLOG. Let us consider a simple statment in PROLOG, such as:
  1463.  
  1464.         1)   has( car, wheels ).
  1465.  
  1466.         This  statement  is a "fact.  The word "has" in this statment  is 
  1467.         known  either  as a functor or predicate.  It is a name  for  the 
  1468.         relationship  within the parenthesis.  It implies that a car  has 
  1469.         wheels.  But  the  order  of  the words  inside  the  bracket  is 
  1470.         arbitrary and established by you. You could just as easily say:
  1471.  
  1472.         2)   has( wheels, car ).
  1473.  
  1474.         and if you wrote this way consistently,  all would be  well.  The 
  1475.         words  has,  wheels,  and car are all PROLOG atoms.  "Wheels" and 
  1476.         "car" are constants. 
  1477.              
  1478.         A   database   of  facts  can  illustrate  the   data   retrieval 
  1479.         capabilities of PROLOG. For instance:
  1480.  
  1481.         3)   has( car, wheels ).
  1482.              has( car, frame ).
  1483.              has( car, windshield ).
  1484.              has( car, engine ).
  1485.  
  1486.         You could then ask PROLOG the question:
  1487.  
  1488.         4)   has( car, Part ).
  1489.  
  1490.         The  capital  "P" of Part means that Part is a  variable.  PROLOG 
  1491.         will make Part equal to whatever constant is required to make the 
  1492.         question match one of the facts in the database. Thus PROLOG will 
  1493.         respond:
  1494.  
  1495.              Part = wheels.
  1496.              
  1497.              More?(Y/N):
  1498.  
  1499.         If you type "y" the next answer will appear:
  1500.  
  1501.              Part = frame.
  1502.  
  1503.              More?(Y/N):
  1504.  
  1505.         If you continue, PROLOG will produce the answers Part = windshield 
  1506.         and Part = engine. Finally, you will see:
  1507.  
  1508.              More?(Y/N):y
  1509.  
  1510.              No.
  1511.          
  1512.         indicating that PROLOG has exhausted the database.  Incidentally, 
  1513.         when  a  variable is set equal to a constant or  other  variable, 
  1514.  
  1515.  
  1516.  
  1517.  
  1518.  
  1519.  
  1520.         it is said to be instantiated to that object.
  1521.  
  1522.              Notice  that  PROLOG searches the database forwards  and  in 
  1523.         this case,  from the beginning.  The forward search path is built 
  1524.         into PROLOG and cannot be changed. An author of a program written 
  1525.         in  a  prescriptive language is quite conscious of the  order  of 
  1526.         execution  of  his program,  while in PROLOG it is  not  directly 
  1527.         under his control.
  1528.              
  1529.              The other major element is the rule which is a fact which is 
  1530.         conditionally true. In logic this is called a Horn clause: 
  1531.  
  1532.  
  1533.         5)   has( X, wheels ) :- iscar( X ).
  1534.  
  1535.         The  fact iscar( car ) and the above rule are equivalent to
  1536.  
  1537.         6)   has( car, wheels).
  1538.  
  1539.         The  symbol :- is the "rule sign." The expression on the left  of 
  1540.         :-is the "head" and on the right is the body.  The variable X has 
  1541.         scope  of the rule,  which means that it has meaning only  within 
  1542.         the rule.  For instance,  we could have two rules in the database 
  1543.         using identically named variables.
  1544.  
  1545.  
  1546.         7)   has( X,  transportation ) :- 
  1547.                            has( X,  car ), has( license, X ).
  1548.  
  1549.         8)   has( X, elephant ) :- istrainer( X ), hasjob( X ).
  1550.  
  1551.         The  variables  X in the two expressions are completely  distinct 
  1552.         and have nothing to do with each other.
  1553.  
  1554.         The comma between has( X, car ) and has( license, X ) means "and" 
  1555.         or logical conjuction.  The rule will not be true unless both the 
  1556.         clauses has(X, car) and has( license, X ) are true.
  1557.  
  1558.  
  1559.              On the other hand if there is a rule
  1560.              
  1561.         9)   has( license, X ) :- passedexam( X ).
  1562.  
  1563.         consider what PROLOG will do in response to the question:
  1564.  
  1565.         10)  has( harry, transportation ).
  1566.  
  1567.         (Notice  that  harry has not been capitalized because we  do  not 
  1568.         want  it  taken as a variable.  We could,  however,  say  'Harry' 
  1569.         enclosed in single quotes.)
  1570.  
  1571.              It  will scan the database and use (7),  in which X will  be 
  1572.         instantiated to harry. The rule generates two new questions:
  1573.  
  1574.         11)  has( harry, car ).
  1575.  
  1576.         12)  has( license, harry ).
  1577.  
  1578.         Assuming  that  harry  has  a car,  the first clause  of  (7)  is 
  1579.         satisfied and the database is scanned for a match to (12). PROLOG 
  1580.  
  1581.  
  1582.  
  1583.  
  1584.  
  1585.  
  1586.         picks  up  rule (9) in which X is instantiated to harry  and  the 
  1587.         question is now posed:
  1588.  
  1589.         13)  passedexam( harry ).
  1590.  
  1591.              If there is a fact:
  1592.  
  1593.              passedexam( harry ).
  1594.  
  1595.         in  the database then all is well and harry  has  transportation. 
  1596.         If there is not, then PROLOG will succinctly tell you:
  1597.  
  1598.              No.
  1599.  
  1600.         But  suppose Harry has money and can hire a chauffer as any  good 
  1601.         programmer  can.  That  could be made part of the program in  the 
  1602.         following way.
  1603.  
  1604.              The rule which PROLOG tried to use was:
  1605.  
  1606.         14)  has( X,  transportation ) :- 
  1607.                            has( X,  car ), has( license, X ).
  1608.  
  1609.         At any point following it there could be included another rule:
  1610.  
  1611.         15)  has( X, transportation ) :- has( X, money ).
  1612.  
  1613.         or simply the bald fact:
  1614.  
  1615.         16)  has( harry, transportation ).
  1616.  
  1617.              These  additional  rules  or  facts would  be  used  in  two 
  1618.         circumstances.  If at any point a rule does not yield a solution, 
  1619.         PROLOG   will  scan  forward  from  that  rule  to  find  another 
  1620.         applicable  one.  This process is known as "backtracking  search" 
  1621.         and can be quite time consuming.
  1622.  
  1623.  
  1624.         If  in response to the "More?" prompt you answer "y" PROLOG  will 
  1625.         search  for an additional distinct solution.  It will attempt  to 
  1626.         find an alternate rule or fact for the last rule or fact used. If 
  1627.         that  fails,  it  will back up to the antecedent rule and try  to 
  1628.         find  an alternate antecedent.  And it will continue to  back  up 
  1629.         until  it  arrives at the question you asked,  at which point  it 
  1630.         will say:
  1631.  
  1632.              No.
  1633.  
  1634.         "Antecedent"  to a rule means that it gave rise to its' use.  For 
  1635.         example,  (7)  is  the antecedent of (9) in the  context  of  the 
  1636.         question (16).
  1637.  
  1638.  
  1639.  
  1640.  
  1641.         II.                          Grammar
  1642.  
  1643.              It is a boring subject, but it must be discussed. All PROLOG 
  1644.         statements are composed of valid terms, possibly a rule sign (":-
  1645.         "),  commas representing conjunction ("and"), and a period at the 
  1646.  
  1647.  
  1648.  
  1649.  
  1650.  
  1651.  
  1652.         very end.
  1653.              A term is a structure, constant, variable, or number.
  1654.          
  1655.              What is a structure? It is a kind of grouping:
  1656.  
  1657.              1) Structures consist of a functor, and a set of objects or
  1658.                 structures in parenthesis.
  1659.  
  1660.              2)  Objects are constants,  variables,  numbers,  or  lists, 
  1661.                 which we have not discussed yet.
  1662.  
  1663.              3)  A  constant or functor must be a string of  letters  and 
  1664.                 numbers, beginning with a lower case letter, unless
  1665.                 you  choose  to  enclose  it in single  quotes  (  'howdy 
  1666.                 pardner'  ),  in  which  case you are  freed  from  these 
  1667.                 restrictions.
  1668.              4) A  variable  must be a string of  letters  and  numbers 
  1669.                 beginning with a capital letter.
  1670.              
  1671.              5) A  functor  may optionally have  arguments  enclosed  in 
  1672.                 parenthesis , as in: hascar( X ) or hascar. 
  1673.  
  1674.         An example:  "has( X, transportation )." is a structure.
  1675.  
  1676.  
  1677.  
  1678.  
  1679.  
  1680.  
  1681.  
  1682.  
  1683.  
  1684.  
  1685.  
  1686.  
  1687.  
  1688.  
  1689.  
  1690.  
  1691.  
  1692.  
  1693.  
  1694.  
  1695.  
  1696.  
  1697.  
  1698.  
  1699.  
  1700.  
  1701.  
  1702.  
  1703.  
  1704.  
  1705.  
  1706.  
  1707.  
  1708.  
  1709.  
  1710.  
  1711.  
  1712.  
  1713.  
  1714.  
  1715.  
  1716.  
  1717.  
  1718.         III.                     Input / Output      
  1719.  
  1720.              You   now   know  enough  to  write  simple  databases   and 
  1721.         interrogate   them  profitably.   But  before  we  examine   more 
  1722.         sophisticated  examples,  it  will be necessary to add input  and 
  1723.         output to the language. There are built in functions which appear 
  1724.         as rules which are satisfied once. Thus the statment:
  1725.  
  1726.              write( 'Hello world.' ).
  1727.  
  1728.         can be included on the right side of a rule:
  1729.  
  1730.  
  1731.         greetings(  X ) :- ishuman( X ),  write( 'Hello world.' ).  You 
  1732.         can  also write "write( X )" where X is some variable.  Note that 
  1733.         'Hello  world.' is not enclosed in double quotes.  Single quotes, 
  1734.         which denote a constant, are required. Double quotes would denote 
  1735.         a list, which is another thing entirely.
  1736.  
  1737.         Provided  that  a match to "ishuman" can be  found,  the  builtin 
  1738.         function  "write"  is  executed and the message  printed  to  the 
  1739.         screen.
  1740.              The  builtin  read( X ) reads a "structure" that  you  input 
  1741.         from the keyboard. More formally, we have
  1742.  
  1743.              read( <variable> or <constant> )
  1744.              write( <variable> or <constant> )
  1745.  
  1746.         If you write read( Input ),  then the variable "keyboard" will be 
  1747.         assigned to whatever is typed at the keyboard,  provided that the 
  1748.         input  is a valid PROLOG structure.  The builtin "read" will fail 
  1749.         if instead of Keyboard we wrote read( baloney ),  where "baloney" 
  1750.         is a constant,  and the user at the keyboard did not type exactly 
  1751.         "baloney." 
  1752.  
  1753.         When you input a structure in response to a "read" statement,  be 
  1754.         sure to end it with a period and an <ENTER>. 
  1755.  
  1756.              There  is  a convenient way of putting the cursor on  a  new 
  1757.         line. This is the builtin "nl". For example:
  1758.  
  1759.              showme :- write( 'line 1' ), nl, write( 'line 2' ). 
  1760.  
  1761.         would result in:
  1762.  
  1763.              line 1
  1764.              line 2
  1765.  
  1766.              There  is  also a primitive form of input/output for  single 
  1767.         characters. It will be discussed later.
  1768.  
  1769.  
  1770.  
  1771.  
  1772.  
  1773.  
  1774.  
  1775.  
  1776.  
  1777.  
  1778.  
  1779.  
  1780.  
  1781.  
  1782.  
  1783.  
  1784.         IV.                   A Fault Tree Example
  1785.  
  1786.              Consider the "won't start" fault tree for an automobile:
  1787.  
  1788.         {Car won't start}
  1789.              | 
  1790.              | 
  1791.         [Engine  turns  over](No) --> [Battery voltage](no)-\
  1792.                 (Yes)                                       v
  1793.                  |                                  {Check battery}
  1794.                  |
  1795.         [Smell gasoline](yes) --> {Try full throttle cranking}
  1796.                  |                       (failure)
  1797.         /--------/                           |
  1798.         |           /------------------------/ 
  1799.         |           |                       
  1800.         |           |
  1801.         |  [Check for fuel line leaks](yes)-->{Replace fuel line}
  1802.         |          (no)
  1803.         |           |
  1804.         |           |
  1805.         |  [Check for defective carburator](yes)--\
  1806.         |          (no)                           v
  1807.         |                                {Repair carburator}
  1808.         \----\
  1809.              |
  1810.              |
  1811.         [Is spark present](no)-->[Do points open and close](no)-\
  1812.              |                             (yes)                v
  1813.         /----/                               |          {Adjust points}
  1814.         |           /------------------------/
  1815.         |           |
  1816.         |  [Pull distributor wire, observe spark](blue)--\
  1817.         |        (orange)                                v
  1818.         |           |                           {Check plug wires & cap}
  1819.         |           |
  1820.         |  [Measure voltage on coil primary](not 12V)--\
  1821.         |         (12V)                                v
  1822.         |           |              {Check wiring, ballast resistor}
  1823.         |           |
  1824.         |  [Check condenser with ohmmeter](conducts)--\
  1825.         |    (no conduction)                          v
  1826.         |           |                         {Replace condenser}
  1827.         |           |
  1828.         |  [Open and close points](voltage not 0 - 12)--\
  1829.         |   (voltage swings 0 - 12)                     v
  1830.         |           |                         {Fix primary circuit}
  1831.         |           |
  1832.         |  {Consider hidden fault, swap components]
  1833.         |
  1834.         |
  1835.         \-------{Call a tow truck!!}
  1836.  
  1837.  
  1838.  
  1839.  
  1840.  
  1841.  
  1842.  
  1843.  
  1844.  
  1845.  
  1846.  
  1847.  
  1848.  
  1849.  
  1850.              A PROLOG program to  implement this is simple. Each statment 
  1851.         represents  a  decision point fragment of the  tree.  The  PROLOG 
  1852.         interpreter  dynamically  assembles  the tree as  it  attempts  a 
  1853.         solution. 
  1854.  
  1855.         'car wont start' :- write( 'Is the battery voltage low?' ), 
  1856.                             affirm, nl,
  1857.                             write( 'Check battery' ).
  1858.  
  1859.         'car wont start' :- write( 'Smell gasoline?' ), 
  1860.                             affirm, nl,
  1861.                             'fuel system'.
  1862.  
  1863.         'fuel system'    :- write( 'Try full throttle cranking' ).
  1864.  
  1865.         'fuel system'    :- write( 'Are there fuel line leaks?' ),
  1866.                             affirm, nl,
  1867.                             write( 'Replace fuel line.' ).
  1868.  
  1869.         'fuel system'    :- write( 'Check carburator' ).
  1870.  
  1871.         'car wont start' :- write( 'Is spark present?' ),
  1872.                             not( affirm ), nl,
  1873.                             'no spark'.
  1874.  
  1875.         'no spark'       :- write( 'Do points open and close?' ),
  1876.                             not( affirm ), nl,
  1877.                             write( 'Adjust or replace points.' ).
  1878.  
  1879.         'no spark'       :- write( 'Is the spark off the coil good?' ),
  1880.                             affirm,
  1881.                             write( 'Check plug wires and cap.' ).
  1882.  
  1883.         'no spark'       :- write( 'What is the voltage on the primary
  1884.                              of the coil: ' ), 
  1885.                             read( Volts ), 
  1886.                             Volts < 10,
  1887.                             nl,
  1888.                             write('Check wiring and ballast resistor.').
  1889.  
  1890.         'no spark'       :- write( 'Does the capacitor leak?' ),
  1891.                             affirm,
  1892.                             write( 'Replace the capacitor.' ).
  1893.  
  1894.         'no spark'       :- not( 'primary circuit' ).
  1895.  
  1896.         'primary circuit' 
  1897.                          :- write( 'Open the  points.  Voltage  across 
  1898.                               coil?:'), nl,
  1899.                             read( Openvolts ), Openvolts < 1, 
  1900.                             write(  'Close the points.  Voltage  across 
  1901.                               coil?:'),
  1902.                             read( Closevolts ), Closevolts > 10, nl,
  1903.                             write( 'Primary circuit is OK.' ). 
  1904.  
  1905.         'no spark'       :- write( 'Consider a hidden fault. Swap
  1906.                               cap, rotor,points,capacitor.' ).
  1907.  
  1908.  
  1909.         'Car wont start' :- write( 'Get a tow truck!!' ).
  1910.  
  1911.  
  1912.  
  1913.  
  1914.  
  1915.  
  1916.  
  1917.  
  1918.                                  --End program--
  1919.  
  1920.  
  1921.              The  above  is  a  simple example of  an  expert  system.  A 
  1922.         sophisticated  system would tell you exactly the method by  which 
  1923.         it  has reached a conclusion.  It would communicate by a  "shell" 
  1924.         program  written  in PROLOG which would accept a wider  range  of 
  1925.         input   than  the  "valid  structure"  required  by  the   PROLOG 
  1926.         interpreter directly.
  1927.  
  1928.  
  1929.  
  1930.  
  1931.  
  1932.  
  1933.  
  1934.  
  1935.  
  1936.  
  1937.  
  1938.  
  1939.  
  1940.  
  1941.  
  1942.  
  1943.  
  1944.  
  1945.  
  1946.  
  1947.  
  1948.  
  1949.  
  1950.  
  1951.  
  1952.  
  1953.  
  1954.  
  1955.  
  1956.  
  1957.  
  1958.  
  1959.  
  1960.  
  1961.  
  1962.  
  1963.  
  1964.  
  1965.  
  1966.  
  1967.  
  1968.  
  1969.  
  1970.  
  1971.  
  1972.  
  1973.  
  1974.  
  1975.  
  1976.  
  1977.  
  1978.  
  1979.  
  1980.  
  1981.  
  1982.         V.                            Lists               
  1983.  
  1984.              Consider  a  shopping list given you by your wife.  It is  a 
  1985.         piece of paper with items written on it in an order that probably 
  1986.         symbolizes  their  importance.  At the top it  may  say  EGGS!!!, 
  1987.         followed by carrots, hamburger, and finally a flea collar for the 
  1988.         dog, if you can find one. In PROLOG such a list would be written:
  1989.  
  1990.         1)   [eggs, carrots, hamburger, fleacollar]
  1991.  
  1992.         The  order of a list is important so that eggs and carrots cannot 
  1993.         be reversed and PROLOG be uncaring.
  1994.  
  1995.         Let us put the list in a structure:
  1996.  
  1997.              shopping( [eggs, carrots, hamburger, fleacollar] ).
  1998.  
  1999.         Then  if you wished to isolate the head of the list you could ask 
  2000.         the question:
  2001.  
  2002.              shopping( [ Mostimportant | Rest ] ).
  2003.  
  2004.         and PROLOG would respond:
  2005.  
  2006.              Mostimportant   =  eggs,   
  2007.              Rest   =   [carrots,   hamburger, fleacollar].
  2008.  
  2009.         The vertical bar "|" is crucial here. It is the string extraction 
  2010.         operator,  which  performs  a  combination  of the  CDR  and  CAR 
  2011.         functions  of LISP.  When it appears in the context [X|Y] it  can 
  2012.         separate the head of the list from the rest, or tail.
  2013.  
  2014.  
  2015.              You  may have gained the impression that PROLOG is a  rather 
  2016.         static language capable of answering simple questions,  but it is 
  2017.         far  more powerful than that.  The string extraction operator  is 
  2018.         the  key.  It permits PROLOG to whittle a complex expression down 
  2019.         to the bare remainder.  If the rules you have given it permit  it 
  2020.         to  whittle  the  remainder  down to  nothing,  then  success  is 
  2021.         achieved. An example of this is the definition of "append."
  2022.  
  2023.              Let  us suppose you have not yet done yesterday's  shopping, 
  2024.         let alone today's. You pull it out of your wallet and sootch tape 
  2025.         it to the list your wife just gave you. Yesterday's list was:
  2026.  
  2027.              [tomatoes, onions, ketchup]
  2028.  
  2029.         Combined with [eggs, carrots, hamburger, fleacollar] we obtain
  2030.  
  2031.              [eggs,carrots,hamburger,fleacollar,tomatoes,onions,garlic].
  2032.  
  2033.         To  take one list and to attach it to the tail of another list is 
  2034.         to  "append"  the first to the second.  The PROLOG definition  of 
  2035.         append is:
  2036.  
  2037.  
  2038.  
  2039.         Rule1:     append( [], L, L ).
  2040.  
  2041.         Rule2:     append( [X|List1], List2, [X|List3] ) :-
  2042.  
  2043.  
  2044.  
  2045.  
  2046.  
  2047.  
  2048.                       append( List1, List2, List3 ].
  2049.  
  2050.         The  general  scheme is this:  
  2051.  
  2052.         The definition consists of one rule and one fact.  The rule  will 
  2053.         be used over and over again until what little is left matches the 
  2054.         fact.  The [] stands for empty list,  which is like a bag without 
  2055.         anything in it. This is an example of a recursive definition.
  2056.              Suppose we ask:
  2057.  
  2058.              append( [a,b,c], [d,e,f], Whatgives ).
  2059.  
  2060.         1. Rule 2 is invoked with arguments ( [a,b,c], [d,e,f], Whatgives ).
  2061.         2. Rule 2 is invoked again with arguments:
  2062.              ( [b,c], [d,e,f], List3 ).
  2063.         3. Rule 2 is invoked again with arguments:
  2064.              ( [b], [d,e,f], List3 ).
  2065.         4.  The  arguments  are now ([],  [d,e,f],  List3 ).  Rule 1  now 
  2066.             matches. End.
  2067.  
  2068.         How does this cause a list to be constructed? The key is to watch 
  2069.         the   third  argument.   Supplied  by  the  user,   it  is  named 
  2070.         "Whatgives". The inference engine matches it to [X|List3] in rule 
  2071.         2. Now lets trace this as rule two is successivly invoked: 
  2072.  
  2073.  
  2074.                 Whatgives                                                
  2075.                    |                                                     
  2076.                    |                                                     
  2077.                    |                                                     
  2078.                    v                                                     
  2079.         Rule2:  [X|List3] (List3 = [b,c])                                
  2080.                  |  \                                                    
  2081.                  |   \                                                   
  2082.                  |    \                                                  
  2083.                  v     \                                                 
  2084.         Rule2:   a   [X'|List3'] (List3' = [c])                          
  2085.                       |    \                                             
  2086.                       |     \                                            
  2087.                       |      \                                           
  2088.                       v       \                                          
  2089.         Rule2:        b     [X''|List3''] (List3'' = [], ie., empty set.)
  2090.                               |    \                                     
  2091.                               |     \                                    
  2092.                               |      \                                   
  2093.         Rule1:                c       L  ( in Rule1 = [d,e,f] )              
  2094.                                                                          
  2095.         End.
  2096.  
  2097.  
  2098.         L in rule 1 is [d,e,f] for the following reason: Notice that rule 
  2099.         2 never alters List2. It supplies it to whatever rule it invokes. 
  2100.         So L in rule 1 is the original List2, or [a,b,c].
  2101.  
  2102.              This example would not have worked if the order of rules one 
  2103.         and  two  were  reversed.  The  PROLOG  inference  engine  always 
  2104.         attempts to use the the first rule encountered. You could imagine 
  2105.         it as always reading your program from the top down in attempting 
  2106.         to  find an appropriate rule.  Since rule 2 would always satisfy, 
  2107.         an  unpleasant  thing  would  have happened  if  the  order  were 
  2108.  
  2109.  
  2110.  
  2111.  
  2112.  
  2113.  
  2114.         reversed. The program would loop forever.
  2115.              
  2116.  
  2117.  
  2118.  
  2119.              I  hope  that  this tiny introduction to PROLOG  whets  your 
  2120.         appetite. You should now purchase the book
  2121.  
  2122.              Programming In Prolog
  2123.              W.F. Clocksin and C.S. Mellish
  2124.              Springer - Verlag
  2125.              Berlin,Heidelberg,New York. 1981,1984
  2126.  
  2127.  
  2128.  
  2129.  
  2130.  
  2131.  
  2132.  
  2133.  
  2134.  
  2135.  
  2136.  
  2137.  
  2138.  
  2139.  
  2140.  
  2141.  
  2142.  
  2143.  
  2144.  
  2145.  
  2146.  
  2147.  
  2148.  
  2149.  
  2150.  
  2151.  
  2152.  
  2153.  
  2154.  
  2155.  
  2156.  
  2157.  
  2158.  
  2159.  
  2160.  
  2161.  
  2162.  
  2163.  
  2164.  
  2165.  
  2166.  
  2167.  
  2168.  
  2169.  
  2170.  
  2171.  
  2172.  
  2173.  
  2174.  
  2175.  
  2176.  
  2177.  
  2178.  
  2179.